<?php


namespace app\user\controller;


use app\user\model\OrderModel;
use app\user\model\TermModel;

class Pay extends Base
{
    protected $noNeedLogin = '*';

    //支付完成
    public function index()
    {

        $order_id = input('order_id/d');
        if (empty($order_id)){
            $this->error('没有订单ID');
        }
        $order = OrderModel::find($order_id);
        $order-> switch = 1;
        $order->pay_time = date('Y-m-d H:i:s');
        if ($order->save()){
            $this->success('支付成功');
        }else{
            $this->error('支付失败');
        }
    }

    //收货完成
    public function rece()
    {
        $order_id = input('order_id/d');
        if (empty($order_id)){
            $this->error('没有订单ID');
        }
        $order = OrderModel::find($order_id);
        $order-> switch = 2;
        $order->end_time = date('Y-m-d H:i:s');
        if ($order->save()){
            $this->success('确认收货成功');
        }else{
            $this->error('确认收货失败');
        }
    }

    //租赁期限到期
    public function expire()
    {$order_id = input('order_id/d');
        if (empty($order_id)){
            $this->error('没有订单ID');
        }
        $order = OrderModel::find($order_id);
//        if($order['term_name'] == '一周'){
//            $order->exp_time = date('Y-m-d H:i:s',strtotime("+1 week"));
//        } elseif ($order['term_name'] == '两周'){
//            $order->exp_time = date('Y-m-d H:i:s',strtotime("+2 week"));
//        } elseif ($order['term_name'] == '一个月'){
//            $order->exp_time = date('Y-m-d H:i:s',strtotime("+1 month"));
//        }
        $date = date('Y-m-d H:i:s');
//        echo $order['exp_time'];
        if ($date > $order['exp_time']){
            $order-> switch = 3;
            if ($order->save()){
                $this->success('租赁时间已到期');
            }else{
                $this->error('租赁时间到期修改失败');
            }
        }else{
            $this->error('未到期');
        }
    }

    //已归还
    public function return()
    {
        $order_id = input('order_id/d');
        if (empty($order_id)){
            $this->error('没有订单ID');
        }
        $order = OrderModel::find($order_id);
        $order-> switch = 4;
//        $order->end_time = date('Y-m-d H:i:s');
        if ($order->save()){
            $this->success('确认归还成功');
        }else{
            $this->error('确认归还失败');
        }
    }

    //续租
    public function lease()
    {
        $data = input('post.');
        if (empty($data['order_id'])){
            $this->error('没有订单ID');
        }
        if (empty($data['term_id'])){
            $this->error('没有租赁期限ID');
        }
        $order = OrderModel::find($data['order_id']);

        $term = TermModel::find($data['term_id']);
        $date = date('Y-m-d H:i:s');
        if ($date > $order['exp_time']){
            //到期时间
            if($term['term'] == '一周'){
                $exp_time = date('Y-m-d H:i:s',strtotime("+1 week"));
            } elseif ($term['term'] == '两周'){
                $exp_time = date('Y-m-d H:i:s',strtotime("+2 week"));
            } elseif ($term['term'] == '一个月'){
                $exp_time = date('Y-m-d H:i:s',strtotime("+1 month"));
            }
            $order->exp_time = $exp_time;
            $order-> switch = 2;
            if ($order->save()){
                $this->success('继租成功');
            }else{
                $this->error('继租失败');
            }
        }else {
            $this->error('不需要续租');
        }

    }



}